//
//  MobilePhoneChargeConfirmViewController.m
//  ELife
//
//  Created by  刘奇男 on 11-6-14.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MobilePhoneChargeConfirmViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "MobilePhoneChargeUnionPayViewController.h"

#import "ELifeAppDelegate.h"
#import "MobilePhoneChargeConfirmViewController.h"
#import "GDataXMLNode.h"
#import "GTMBase64.h"
#import "GetMyData.h"
#import "LoginController.h"
#import "AlertView.h"


@implementation MobilePhoneChargeConfirmViewController

@synthesize cost, phoneNumber, accountBalance, phoneInfoBack, helpBack,costValue;

//3.2 手机话费充值接口
//请求地址: /phonepay.php
//请求参数: action  => phone_charge
//user_id  => 用户id
//version	 => 客户端版本号
//phone	 => 用户手机号
//money	 => 用户手机号充值的面额.
//返回参数:
//user_id  => 用户id
//time	 => 话费到帐时间
//phone	 => 用户手机号
//money	 => 用户手机号充值的面额.


- (void)charge
{
    NSString* userid = [GetMyData getUserId];
    
    if (userid == nil) {
        LoginController* lc = [[LoginController alloc] init:self];
        [self.navigationController pushViewController:lc animated:YES];
        [lc release];
        return;
    }
    
    
    NSString* host =  [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil];
    
    NSURL* url = [[NSURL alloc] initWithString:[host stringByAppendingString:@"phonepay.php"]];
    
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:2];
    [param setObject:@"phone_charge" forKey:@"action"];
    [param setObject: [GetMyData getUserId] forKey:@"user_id"];
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    [param setObject:phoneNumber.text forKey:@"phone"];
    [param setObject:self.costValue forKey:@"money"];

    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestChargeFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[[ELifeAppDelegate App] httpRequestPoint]openUrl:dic postParam:param target:self];
    
}

- (void)iRequestChargeFinished:(NSData *)response
{
    [[ELifeAppDelegate App] hideProgressDialog];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:response] options:0 error:nil];
    
    
    GDataXMLElement* rootElement = [doc rootElement];    
    NSLog(@"-----rootElement-----%@\n", rootElement);
    
    
    GDataXMLElement* headsElement = [[rootElement elementsForName:@"head"]objectAtIndex:0];
    NSLog(@"-----headsElement-----%@\n", headsElement);
    
    GDataXMLNode* returnCode = [[headsElement elementsForName:@"returncode"] objectAtIndex:0];
    NSInteger code = [[returnCode stringValue] intValue];
    if (code == -105){
        //        [AlertView showAlert:@"请先登录" action:@"goLogin"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录"
                                                       delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
        [alert show];
        [alert setTag:11];
        [alert release];
        
        [doc release];
        return;
    } else if (code != 0 ){
        [AlertView showAlert:@"充值失败"];
        [self.navigationController popViewControllerAnimated:YES];
        
        [doc release];
        return;
    }
    
    
    GDataXMLElement* bodyElement = [[rootElement elementsForName:@"body"]objectAtIndex:0];
    
    NSLog(@"-----rootElement-----%@\n", bodyElement);
//	<?xml version="1.0" encoding="UTF-8"?><msg><head><returncode>0</returncode><transcode>phone_charge</transcode></head><body><user_id>1</user_id><time>5分钟</time><phone>13810605784</phone><money>100</money></body></msg>

    [doc release];
    
    [AlertView showAlert:@"充值成功"];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)search
{
    
    NSString* userid = [GetMyData getUserId];
    
    if (userid == nil) {
        LoginController* lc = [[LoginController alloc] init:self];
        [self.navigationController pushViewController:lc animated:YES];
        [lc release];
        return;
    }
    
    NSString* host =  [[NSBundle mainBundle] localizedStringForKey:(@"BussinessHost") value:@"" table:nil];
    
    NSURL* url = [[NSURL alloc] initWithString:[host stringByAppendingString:@"user.php"]];
    
    
    NSMutableDictionary *param = [[NSMutableDictionary alloc]initWithCapacity:2];
    [param setObject:@"getaccountinfo" forKey:@"action"];
    [param setObject: [GetMyData getUserId] forKey:@"user_id"];
    [param setObject:[[NSBundle mainBundle] localizedStringForKey:(@"ClientVersion") value:@"" table:nil] forKey:@"version"];
    
    NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:url, @"iRequestDetailFinished:", nil] forKeys:[NSArray arrayWithObjects:@"url", @"requestMethod", nil]];
    
    [[[ELifeAppDelegate App] httpRequestPoint]openUrl:dic postParam:param target:self];
    
}

- (void)iRequestDetailFinished:(NSData *)response
{
    [[ELifeAppDelegate App] hideProgressDialog];
    GDataXMLDocument *doc = [[GDataXMLDocument alloc] initWithData:[GTMBase64 decodeData:response] options:0 error:nil];
    
    
    GDataXMLElement* rootElement = [doc rootElement];    
    NSLog(@"-----rootElement-----%@\n", rootElement);
    
    
    GDataXMLElement* headsElement = [[rootElement elementsForName:@"head"]objectAtIndex:0];
    NSLog(@"-----headsElement-----%@\n", headsElement);
    
    GDataXMLNode* returnCode = [[headsElement elementsForName:@"returncode"] objectAtIndex:0];
    NSInteger code = [[returnCode stringValue] intValue];
    if (code == -105){
        //        [AlertView showAlert:@"请先登录" action:@"goLogin"];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请先登录"
                                                       delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
        [alert show];
        [alert setTag:11];
        [alert release];
        return;
    } else if (code != 0 ){
        [AlertView showAlert:@"获取账户信息信息失败"];
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    

    GDataXMLElement* bodyElement = [[rootElement elementsForName:@"body"]objectAtIndex:0];
    
    //    <msg><head><returncode>0</returncode><transcode>getaccountinfo</transcode></head><body><user_id>318813</user_id><score>135</score><account>0.0000</account><daijingquan>0.0000</daijingquan></body></msg>
    
    //某个具体节点的文本内容
    NSString* score = [[[bodyElement elementsForName:@"score"]objectAtIndex:0] stringValue];
    NSString* account = [[[bodyElement elementsForName:@"account"]objectAtIndex:0] stringValue];
    NSString* daijingquan = [[[bodyElement elementsForName:@"daijingquan"]objectAtIndex:0] stringValue];
    
    [self.accountBalance setText:[NSString stringWithFormat:@"%@元", account]];
    
    [doc release];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    
    [cost release];
    [phoneNumber release];
    [accountBalance release];
    [phoneInfoBack release];
    [helpBack release];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
}
*/

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
 
    phoneInfoBack.layer.cornerRadius = 8;
    phoneInfoBack.layer.masksToBounds = YES;
    helpBack.layer.cornerRadius = 8;
    helpBack.layer.masksToBounds = YES;
    
    [self search];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) setUserPhoneNumber:(NSString*) phoneNumber
{
    [self.phoneNumber setText:phoneNumber];
}
-(void) setUserType:(NSString*) type
{
    self.costValue = type;
    [self.cost setText:[NSString stringWithFormat:@"%@元", type]];
}

-(IBAction) accountBalancePay:(id)sender
{
    [self charge];
}

-(IBAction) unionPay:(id)sender
{
    MobilePhoneChargeUnionPayViewController *controler = [[MobilePhoneChargeUnionPayViewController alloc] initWithNibName:@"MobilePhoneChargeUnionPayView" bundle:nil];
    [self.navigationController pushViewController:controler animated:YES];
    [controler release];
}

@end
